<template>
	<div class="commit-footer">
		<div class="addr_foot" v-if="showFooter">
			<div class="addr_foot_f">
				<div><span>当前城市：</span><span>{{$store.state.CurrentCity}}</span></div>
				<div><span>当前区县：</span><span style='color: red;'>{{$store.state.CurrentDistrict}}</span></div>
				<div><span>当前地址：</span><span>{{$store.state.CurrentLocation}}</span></div>
			</div>
			<div class="addr_foot_s">
				<div @click="RefreshPositioning">刷新定位</div>
			</div>
		</div>
		<div class="left-countbox" v-if="showFooter">
			<div class="left-count">
				<div class="top">
					共 &nbsp; <i> {{historyLabelNo.length + notScanLabelNo.length + count.length}}</i> 票
				</div>
				<!-- <div class="top">
					已扫描数量:<i> {{historyLabelNo.length}}</i>件
				</div>
				<div class="bottom">
					当前扫描:<i>{{ count.length }}</i>件
				</div> -->
			</div>
			<div class="commit-button-box">
				<!-- <div v-if='orderListScanned.length < 1 || (orderListScanned.length == 1 && orderListScanned[0].flag != "package") '
					class="commit-button BatchCommit" @click='BatchCommit'>批量提交</div> -->
				<div class="commit-button" :class='{checkForCommit:hideBitch}' @click="checkForCommit2">提交</div>
			</div>
			<v-confirm ref="myConfirm" @userBehavior="userBehaviorFun"></v-confirm>
		</div>
		<!-- 批量提交修改数量弹出框 -->
		<div class="BatchCommitPopBox" v-show='BatchCommit_show'>
			<div class="BatchCommit_">
				<div class="BatchCommit_head">请填写要批量提交的数量</div>
				<div class="BatchCommit_info">
					<span>母单号：</span><span>{{orderListScanned.length?orderListScanned[0].order:0}}</span></div>
				<div class="BatchCommit_info"><span>订单总件数：</span><span>{{BatchOrderNum}}</span></div>
				<div class="BatchCommit_info"><span>上次已扫描数量：</span><span>{{BatchOrderLastNum}}</span></div>
				<div class="BatchCommit_content">
					<!-- <span>批量提交数量：</span> -->
					<input type="number" pattern="[0-9]*" v-model='BatchCommitNumber'>
				</div>
				<div class="BatchCommit_btn">
					<div class="commit-button" @click='BatchCommitCancel'>取消</div>
					<div class="commit-button" @click='BatchCommitConfirmbefore'>批量提交</div>
				</div>
			</div>
		</div>
		<!-- 提交前 全部扫描后 提示弹出框 -->
		<div class="SelectDelivery" v-if='AllCommitPopUp_show'>
			<div class="DeliveryBox">
				<!-- <div class="DeliveryBox_head commitPopUp_head" style="color: red;border: none;">是否提交数据？</div> -->
				<div class="DeliveryBox_head commitPopUp_head" style="color: red;border: none;">请确认地址是否正确？</div>
				<div class="DeliveryBox_head commitPopUp_head" style="color: red;border: none;">不正确请刷新定位<span
						class="addr_foot_sl" @click="RefreshPositioning">刷新定位</span></div>
				<div class="DeliveryBox_content commitPopUp_content">
					<div style="border: none;"><span>当前城市：</span>{{$store.state.CurrentCity}}</div>
					<div style="border: none;"><span>当前区县：</span><span style='color: red;'>{{$store.state.CurrentDistrict}}</span>
					</div>
					<div style="border: none;"><span>当前地址：</span>{{$store.state.CurrentLocation}}</div>
				</div>
				<div class="DeliveryBox_bottom">
					<div><span @click="AllCcommitPopUpCancel">取消</span></div>
					<div><span @click="AllCcommitPopUpConfirm">确定</span></div>
				</div>
			</div>
		</div>
		<div v-if="ifTips" class="msg_tips">
			<div class="tips_content">
				<i class="iconfont icon-chenggong1"></i>
				<p>{{tips_Msg}}</p>
			</div>
		</div>
	</div>
</template>
<script>
	import {
        aviationOutPortSubmitScan,
        aviationOutPortBatchSubmitScan
	} from "../api/api";
	import Confirm from '../components/confirm'
	import $ from 'jquery'
	export default {
		name: "commit-footer",
		components: {
			'v-confirm': Confirm
		},
		data() {
			return {
				tips_Msg: '',
				ifTips: false,
                docmHeight: document.documentElement.clientHeight,  // 默认屏幕高度       
                realHeight:  0, // 实时屏幕高度 
                showFooter: true, // 显示底部fixed
				CurrentCity: '', // 当前城市
				CurrentLocation: '', // 当前地址
				CurrentDistrict: '', // 当前区县
				BatchCommit_show: false, // 批量提交修改数量弹出框 是否出现
				BatchCommitNumber: '', // 批量提交数量
				longitude: '',
				latitude: '',
				crruentconfirm: "", // 是提交还是批量提交
				AllCommitPopUp_show: false, // 提交前 全部扫描 提示弹出框
				BatchOrderNum: 0, // 批量提交时，当前订单总件数
				BatchOrderLastNum: 0 // 批量提交时，当前订单 上次提交的数量
			}
		},
		props: [
			'vehicleNo',
			'address',
			'count', // 运单数量
			'orderCount', // 订单数量
			'countData',
			'orderList',
			'deliveryId',
			'hideBitch',
			'transportLinks', // 扫描环节
			'actualQty', // 订单总件数
			'orderId', // 订单id
			'AbHasScanNum', // 异常已签收数量
			'yiScanNumber', // 上次已扫描数量
			'scanned', // 历史列表
			'notScanned', // 未扫描标签列表
			'orderListScanned', // 已扫描标签列表
			'OMSNum', // 订单数量
			'historyLabelNo', // 历史标签列表
			'notScanLabelNo' // 未扫描标签列表
        ],
        watch: {
            realHeight:function() {
                // 如果默认高度大于实际高度则影藏，反之就显示
				if (this.docmHeight > this.realHeight) {            
					this.showFooter = false        
				} else {            
					this.showFooter = true        
				}
            }
        },
		mounted() {
			let CurrentLocationInfo = this.Geolocation('positionInfo'); 
			// this.onPlusReady('e') // 获取APP经纬度
			this.CurrentCity = this.$store.state.CurrentCity
			this.CurrentDistrict = this.$store.state.CurrentDistrict
			this.CurrentLocation= this.$store.state.CurrentLocation
			this.latitude = this.$store.state.latitude
			this.longitude = this.$store.state.longitude
            window.onresize = ()=>{
                return(()=>{                     
                    // 实时屏幕高度                       
                    this.realHeight = document.body.clientHeight         
                })()    
            } 
		},
		methods: {
			// 刷新定位
			RefreshPositioning() {
				this.onPlusReady('e') // 获取APP经纬度
			},
			SetCurrAddress(lngad) {
				this.ReceivingLatitud(lngad);
			},
			// 批量提交弹出框出现
			BatchCommit() {
				var that = this,
					scannedNum = 0,
					notScannedNum = 0;
				if (this.count.length === 0) {
					return;
				}
        for (let lp = 0; lp < that.orderListScanned.length; lp++) {
          if (that.orderListScanned[lp].businessType == '专享急件' && $('.Upload').eq(lp).find('div.uploadImg ul>li').length < 1) {
            that.$alert('专享急件必须拍照上传，才能提交', '提示', {
              confirmButtonText: '确定'
            })
            return
          }
        }
				if (that.orderListScanned.length > 1) {
					return;
				}
				this.onPlusReady('isLoading_')
				for (let j = 0; j < that.scanned.length; j++) {
					if (that.scanned[j].omsNo == that.orderListScanned[0].order) {
						scannedNum = that.scanned[j].waybillNumber.length;
					}
				}
				for (let d = 0; d < that.notScanned.length; d++) {
					if (that.notScanned[d].omsNo == that.orderListScanned[0].order) {
						notScannedNum = that.notScanned[d].waybillNumber.length;
					}
				}
				this.BatchOrderNum = that.orderListScanned[0].waybillNumber.length + scannedNum + notScannedNum;
				this.BatchOrderLastNum = scannedNum;
				this.BatchCommit_show = true;
				this.crruentconfirm = "批量";
				this.$emit('scanFun', false) // 关闭摄像头
			},
			// 批量提交弹出框 取消按钮
			BatchCommitCancel() {
				this.BatchCommit_show = false;
				this.$emit('scanFun', true) // 打开摄像头
			},
			BatchCommitConfirmitem() {
				let that = this;
				that.AllCommitPopUp_show = false;
				that.BatchCommit_show = false;
				let postData = {
					cellphone: that.$store.state.userInfo.cellphone, // 扫描人的电话 *
					orderNo: that.count[0].omsno, // 订单号
					labelNum: that.BatchCommitNumber, // 数量
					roteInfo: {
						address: that.CurrentLocation,
						city: that.CurrentCity,
						district: that.CurrentDistrict,
						latitude: that.latitude,
						longitude: that.longitude
					}
				};
                this.httpRequest1('post', aviationOutPortBatchSubmitScan, postData, 'isLoading_').then(res => {
					if ($("div.uploadImg ul>li").length > 0) {
						that.$parent.updateimg();
						that.$parent.isBatch = true;
					} else {
						that.$confirm(res.detail + ' 是否打印分拣标签？', '提示', {
							confirmButtonText: '打印',
							cancelButtonText: '放弃',
							type: 'warning'
						}).then(() => {
							let json = '';
							for (let i = 0; i < that.orderListScanned.length; i++) {
								let order = that.orderListScanned[i];
								order.p_tags = []
								for (let j in that.orderListScanned[i].printInfo) {
									that.orderListScanned[i][j]= that.orderListScanned[i].printInfo[j]
								}
								// that.orderListScanned[i].p_tags = 
								for (let k in that.orderListScanned[i].printInfo.labelInfo) {
									let infoitem = that.orderListScanned[i].printInfo.labelInfo[k];
									order.p_tags.push({
										"sortNum": infoitem,
										"productNo": k
									})
								}
							}
							json = JSON.stringify(that.orderListScanned)
							// 安卓
							try {
								CallAndroidOrIOS.callAndroid("航空标签打印", json);
							} catch (error) {}
							// 苹果
							try {
								window.webkit.messageHandlers.messageSend.postMessage({
								a: '航空标签打印',
								b: json
								})
							} catch (error) {}
							this.$emit('reflash', true)
						}).catch(() => {
							this.$emit('reflash', true)
						});
					}
				})
			},
			// 批量提交前
			BatchCommitConfirmbefore() {
				let that = this;
				if (!this.BatchCommitNumber) {
					return;
				}
				// 判断输入的是否为数字，不是数字则return
				if (parseInt(this.BatchCommitNumber).toString() == "NaN" || parseInt(this.BatchCommitNumber) <= 0 || this
					.BatchCommitNumber != parseInt(this.BatchCommitNumber)) {
					that.$alert("请输入正整数", '提示', {
						confirmButtonText: '确定'
					})
					return;
				}
				if (that.BatchCommitNumber > (that.BatchOrderNum - that.BatchOrderLastNum)) {
					that.$alert("批量提交数量不能超过剩余未扫描总件数", '提示', {
						confirmButtonText: '确定'
					})
					return;
				}
				if (that.BatchCommitNumber < that.BatchOrderNum) {
					that.$alert("子母件分离，请逐个扫描", '提示', {
						confirmButtonText: '确定'
					})
					return;
				}
				this.AllCommitPopUp_show = true;
			},
			userBehaviorFun(type, data) {
				if (type !== 'clickCancel') {
					this.commitData()
				} else {
					this.$emit('scanFun', true)
				}
			},
			// 提交前 完全扫描 弹出框 消失
			AllCcommitPopUpCancel() {
				this.AllCommitPopUp_show = false;
			},
			// 提交前 完全扫描 弹出框 确定
			AllCcommitPopUpConfirm() {
				if (this.crruentconfirm == "批量") {
					this.BatchCommitConfirmitem();
				} else {
					this.commitData();
				}
			},
			// 判断路由，提货扫描以及签收扫描弹出弹窗  
			checkForCommit2 () {
				let omsNoArr = []
				this.orderListScanned.map(item => {
					omsNoArr.push({'omsNo': item.omsNo, 'labelNo': item.labelNo, 'orderId': item.id, orderType: item.tempFieldOne})
				})
				let that = this
				let params = {
					"gps": {
						"latitude": this.$store.state.latitude,
						"longitude": this.$store.state.longitude, 
						"address": this.$store.state.CurrentLocation,
						"city": this.$store.state.CurrentCity,
						"districict": this.$store.state.CurrentDistrict
					},
					"selectAddress": this.address,
					"labelType":"装车扫描",
					"omsNoArray": omsNoArr,
					"vehicleNo": this.vehicleNo
				}
				that.httpRequest1('post', '/system/app/saveScanLabelApp', params,'isLoading_').then(res => {
					if (res.success) {
						that.ifTips = true;
						that.tips_Msg = '提交成功';
						that.$emit('reflash', true)
						setTimeout(function () {
							that.ifTips = false;
						}, 1500)
					}
				})
			},
			checkForCommit() {
        let that = this;
				if (this.count.length !== 0) {
          for (let lp = 0; lp < that.orderListScanned.length; lp++) {
            if (that.orderListScanned[lp].businessType == '专享急件' && $('.Upload').eq(lp).find('div.uploadImg ul>li').length < 1) {
              that.$alert('专享急件必须拍照上传，才能提交', '提示', {
                confirmButtonText: '确定'
              })
              return
            }
          }
					this.onPlusReady('isLoading_')
					this.crruentconfirm = "提交";
					this.AllCommitPopUp_show = true;
					this.$emit('scanFun', false) // 关闭摄像头
				}
			},
			/**
			 * 数据提交
			 */
			commitData() {
				let that = this;
				that.AllCommitPopUp_show = false;
				let params = {}
				params.cellphone = this.$store.state.userInfo.cellphone // 扫描人的电话 *
				params.roteInfo = {
					address : this.CurrentLocation,
					city : this.CurrentCity,
					district : this.CurrentDistrict,
					latitude : this.latitude,
					longitude : this.longitude
				}
                params.orderList = []
				for (let key in this.orderListScanned) {
					params.orderList.push({
						ordNo: this.orderListScanned[key].order,
						labelList: this.orderListScanned[key].waybillNumber
					})
				}
                that.httpRequest1('post', aviationOutPortSubmitScan, params,'isLoading_').then(res => {
                    if ($("div.uploadImg ul>li").length > 0) {
                        that.$parent.updateimg();
                    } else {
                        that.$emit('isLoading_', false);
						that.$confirm(res.detail + ' 是否打印分拣标签？', '提示', {
							confirmButtonText: '打印',
							cancelButtonText: '放弃',
							type: 'warning'
						}).then(() => {
							let json = '';
							for (let i = 0; i < that.orderListScanned.length; i++) {
								let order = that.orderListScanned[i];
								order.p_tags = []
								for (let j in that.orderListScanned[i].printInfo) {
									that.orderListScanned[i][j]= that.orderListScanned[i].printInfo[j]
								}
								for (let k in that.orderListScanned[i].printInfo.labelInfo) {
									let infoitem = that.orderListScanned[i].printInfo.labelInfo[k];
									that.orderListScanned[i].waybillNumber.filter((item) =>{
										if (item == k) {
											order.p_tags.push({
												"sortNum": infoitem,
												"productNo": item
											})
										}
									})
								}
							}
							json = JSON.stringify(that.orderListScanned)
							// 安卓
							try {
								CallAndroidOrIOS.callAndroid("航空标签打印", json);
							} catch (error) {}
							// 苹果
							try {
								window.webkit.messageHandlers.messageSend.postMessage({
								a: '航空标签打印',
								b: json
								})
							} catch (error) {}
							this.$emit('reflash', true)
						}).catch(() => {
							this.$emit('reflash', true)
						});
                    }
                    this.$emit('scanFun', true)
				}).catch(res => {
                    if (res.code && res.code != 0) {
                        this.$emit('scanFun', true)
                    }
                })
			}
		}
	}
</script>
<style scoped lang="less">
	@import "publicLess.less";
	.SelectDelivery {
		.DeliveryBox {
			padding-top: 30/50rem;
			.commitPopUp_head {
				height: 50/50rem;
				line-height: 50/50rem;
			}
		}
	}
</style>